package com.itheima.java.ai.langchin4j.store;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
@ConditionalOnProperty(name = "spring.profiles.active", havingValue = "dev")
public class InMemoryChatMemoryStore implements ChatMemoryStore {
    
    private final ConcurrentMap<Object, List<ChatMessage>> memoryStore = new ConcurrentHashMap<>();
    
    @Override
    public List<ChatMessage> getMessages(Object memoryId) {
        return memoryStore.getOrDefault(memoryId, new LinkedList<>());
    }
    
    @Override
    public void updateMessages(Object memoryId, List<ChatMessage> messages) {
        memoryStore.put(memoryId, new LinkedList<>(messages));
    }
    
    @Override
    public void deleteMessages(Object memoryId) {
        memoryStore.remove(memoryId);
    }
}